package com.zdy.showgoods;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.networklibrary.BaseObserver;
import com.example.networklibrary.MD5;
import com.example.networklibrary.RxHttp;
import com.example.networklibrary.SharedPreferencesUtils;
import com.example.networklibrary.config.KeyInterface;
import com.example.networklibrary.entity.RequestEntity;
import com.example.networklibrary.model.ProductRecordModel;
import com.zdy.adapter.ProductRecordListAdapter;
import com.zdy.modellibrary.base.BaseActivity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ActivityCallback extends BaseActivity implements View.OnClickListener {
    private ImageView product_callback_back;
    private RecyclerView product_callback_list;
    private ProductRecordListAdapter productRecordListAdapter;
    private List<ProductRecordModel> mDataList;
    private int indexPage = 1;//当前页码
    private int maxPage = 1;//最大页码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_callback);
        initView();
        loadHistory(indexPage);
    }

    private void initView() {
        mDataList = new ArrayList<>();
        product_callback_back = findViewById(R.id.product_callback_back);
        product_callback_list = findViewById(R.id.product_callback_list);
        product_callback_back.setOnClickListener(this);
        product_callback_list.setLayoutManager(new LinearLayoutManager(this));
        product_callback_list.setAdapter(productRecordListAdapter = new ProductRecordListAdapter(this, mDataList));
        productRecordListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {

            @Override
            public void onLoadMoreRequested() {
                indexPage++;
                if (indexPage > maxPage) {
                    productRecordListAdapter.loadMoreEnd();//结束加载
                    return;
                }
                productRecordListAdapter.loadMoreEnd(true);
            }
        }, product_callback_list);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.product_callback_back) {
            finish();
        }
    }


    private void loadHistory(int indexPage) {
        RequestEntity requestEntity = new RequestEntity();
        JSONObject id = new JSONObject();
        id.put("history_id", "");
        requestEntity.setBody(id);
        JSONObject header = new JSONObject();
        header.put("request_seq", KeyInterface.REQUEST_SEQ);
        header.put("sign", "");
        header.put("sys_id", KeyInterface.SYS_ID);
        header.put("curr_page", indexPage);//当前页码
        header.put("page_size", 20);//每页数量
        header.put("tra_code", KeyInterface.SERVICE_HISTORY);
        requestEntity.setHeader(header);

        try {
            String signString = JSONObject.toJSONString(requestEntity);
            String sign = MD5.getSign(signString);
            header.put("sign", sign);
            requestEntity.setHeader(header);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String json = "";
        try {
            json = URLEncoder.encode(JSONObject.toJSONString(requestEntity), "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        RxHttp.getInstance()
                .getSyncServer()
                .parseArray(SharedPreferencesUtils.getString(this, KeyInterface.AppURL), json)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<JSONArray>() {
                    @Override
                    public void onSuccess(JSONArray result) {
                        try {
                            productRecordListAdapter.loadMoreComplete();
                            productRecordListAdapter.disableLoadMoreIfNotFullPage();
                            parseData(result);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        productRecordListAdapter.loadMoreComplete();
                        productRecordListAdapter.disableLoadMoreIfNotFullPage();
                    }
                });
    }

    private void parseData(JSONArray result) {
        maxPage++;
        mDataList.addAll(JSONArray.parseArray(result.toJSONString(), ProductRecordModel.class));
    }
}
